
package com.pp.admin.main.client.common;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.pp.admin.common.client.beans.PageInfo;
import com.pp.admin.common.client.helper.AdminImage;
import com.pp.admin.common.client.helper.Resource;

public class Pagination extends HorizontalPanel implements ClickHandler {
	
	private AdminImage		first	= new AdminImage( Resource.FIRST_IMG_URL, PageInfo.FIRST + "" );
	
	private AdminImage		prev	= new AdminImage( Resource.PREV_IMG_URL, PageInfo.PREV + "" );
	
	private AdminImage		next	= new AdminImage( Resource.NEXT_IMG_URL, PageInfo.NEXT + "" );
	
	private AdminImage		last	= new AdminImage( Resource.LAST_IMG_URL, PageInfo.LAST + "" );
	
	private PageInfo		page	= null;
	
	private TabListContents	parent	= null;
	
	public Pagination( TabListContents parent, PageInfo page ) {

		this.parent = parent;
		first.addClickHandler( this );
		prev.addClickHandler( this );
		next.addClickHandler( this );
		last.addClickHandler( this );
		refresh( page );
	}
	
	public void refresh( PageInfo page ) {

		this.clear();
		this.page = page;
		if ( page.getCurrentPage() == 1 ) {
			first.setUrl( Resource.DISABLE_FIRST_IMG_URL );
			prev.setUrl( Resource.DISABLE_PREV_IMG_URL );
		} else {
			first.setUrl( Resource.FIRST_IMG_URL );
			prev.setUrl( Resource.PREV_IMG_URL );
			
		}
		
		if ( page.getCurrentPage() == page.getNoOfPages() ) {
			next.setUrl( Resource.DISABLE_NEXT_IMG_URL );
			last.setUrl( Resource.DISABLE_LAST_IMG_URL );
		} else {
			next.setUrl( Resource.NEXT_IMG_URL );
			last.setUrl( Resource.LAST_IMG_URL );
		}
		
		this.add( first );
		this.add( prev );
		this.add( new HTML( page.getCurrentPage() + " page of " + page.getNoOfPages() ) );
		this.add( next );
		this.add( last );
		
	}
	
	/**
	 * @return the page
	 */
	public PageInfo getPage() {

		return page;
	}
	
	/*
	 * (non-Javadoc)
	 * @see
	 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent
	 * )
	 */
	@Override
	public void onClick( ClickEvent event ) {

		page.setCurrentAction( Integer.parseInt( ( (AdminImage) event.getSource() ).getValue() ) );
		parent.refresh();
	}
	
}
